import { BaseModel } from './BaseModel.js';
import { dataFiles } from '../config/database.js';

const defaultDormitories = [
  {
    id: 1,
    buildingId: 1,
    buildingName: '紫荆1号楼',
    roomNumber: '101',
    genderType: 'female', // 'male' 或 'female'，继承自宿舍楼
    roomType: '4人间',
    totalBeds: 4,
    occupiedBeds: 0,
    availableBeds: 4,
    floor: 1,
    orientation: '南',
    hasAirConditioner: true,
    hasBalcony: true,
    hasIndependentBathroom: true,
    monthlyRent: 1200,
    status: 'available',
    description: '标准四人间，配备空调、独立卫浴',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  },
  {
    id: 2,
    buildingId: 2,
    buildingName: '银杏2号楼',
    roomNumber: '201',
    genderType: 'male', // 'male' 或 'female'，继承自宿舍楼
    roomType: '6人间',
    totalBeds: 6,
    occupiedBeds: 0,
    availableBeds: 6,
    floor: 2,
    orientation: '北',
    hasAirConditioner: true,
    hasBalcony: false,
    hasIndependentBathroom: false,
    monthlyRent: 800,
    status: 'available',
    description: '经济六人间，配备空调',
    createdAt: new Date().toISOString(),
    updatedAt: new Date().toISOString()
  }
];

export class Dormitory extends BaseModel {
  constructor() {
    super(dataFiles.dormitories, defaultDormitories);
  }

  findByBuilding(buildingName) {
    const data = this.findAll();
    return data.filter(dorm => dorm.buildingName === buildingName);
  }

  findByRoom(buildingName, roomNumber) {
    return this.findOne({ buildingName, roomNumber });
  }

  findAvailableRooms() {
    const data = this.findAll();
    return data.filter(dorm => dorm.status === 'available' && dorm.availableBeds > 0);
  }
}